# Debian Testing version - YafaRay Blender Add-On building procedure (only valid for YafaRay v3.1.2 or higher)
# NOTE: This procedure generates a "yafaray_v3" folder that can be installed in the Blender Add-Ons folder
#       However, this only works for the Blender included in the distro (currently 2.77 in Debian Testing)
#       When using this yafaray_v3 folder with the Blender downloaded from www.blender.org there might be
#       conflicts with the Blender EXR, PNG and JPEG libraries.

# INSTALLING DEVEL TOOLS
sudo apt-get install -y git cmake cmake-curses-gui autoconf gcc g++ cpp swig

# INSTALLING BLENDER
sudo apt-get install -y blender 

# INSTALLING LIBRARY DEPENDENCIES
sudo apt-get install -y python3-dev zlib1g-dev libpng-dev libfreetype6-dev libxml2-dev libtiff5-dev libjpeg-dev libopencv-dev libopenexr-dev

# DOWNLOADING YAFARAY AND SETTING UP FOLDERS
mkdir -p $HOME/yafa-dev/src $HOME/yafa-dev/make $HOME/yafa-dev/build
cd $HOME/yafa-dev/src
git clone https://github.com/YafaRay/Blender-Exporter
git clone https://github.com/YafaRay/Core

# CREATE UserConfig.txt FILE WITH YAFARAY BUILDING OPTIONS. Execute the entire block below until (including) EOF

cat <<EOF >$HOME/yafa-dev/make/UserConfig.txt
####################################################################
# UserConfig File to build for Debian Testing Blender Add-On
####################################################################
# WARNING: DON'T TOUCH THE NEXT LINE                                           
set(YAF_USER_CONFIG ON)
####################################################################

set(DEBUG_BUILD OFF)
set(USER_INSTALL_PREFIX "$HOME/yafa-dev/build/yafaray_v3/")
set(BLENDER_ADDON ON)
set(YAF_PY_VERSION 3.5)
set(BLENDER_EXPORTER_PATH "$HOME/yafa-dev/src/Blender-Exporter")
####################################################################
EOF


# BUILDING YAFARAY
cd $HOME/yafa-dev/make
cmake $HOME/yafa-dev/src/Core
# Note: "-j9" uses 9 threads in this example for speeding up the building process. It can be changed to "-j<number of threads>" depending on the number of cores of the processor used for building YafaRay.
make -j9
make install -j9

# TEST IN BLENDER
mkdir -p $HOME/.config/blender/2.77/scripts/addons
cp -R $HOME/yafa-dev/build/yafaray_v3 $HOME/.config/blender/2.77/scripts/addons/yafaray_v3
blender $HOME/yafa-dev/build/yafaray_v3/tests/test01/test01.blend

# In Blender, go to File->User Preferences->AddOns-->YafaRay v3 and enable it. Then, click save settings and close the User Preferences window.

# Now you should be able to render the test01.blend file.
